import java.util.Map;
import java.util.HashMap;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-09-03
 * Time: 9:41
 */
public class Test {
    public String modifyString(String ss) {
        char[] s = ss.toCharArray();
        int n = s.length;
        for(int i = 0; i < n; i++) {
            if(s[i] == '?') {
                for(char ch = 'a'; ch <= 'z'; ch++) {
                    if((i == 0 || ch != s[i - 1]) && (i == n - 1 || ch != s[i +1])) {
                        s[i] = ch;
                        break;
                    }
                }
            }
        }

        return String.valueOf(s);
    }
    public String convert(String ss, int numRows) {
        if(numRows == 1) return ss;
        char[] s = ss.toCharArray();
        int n = s.length;
        int d = 2 * numRows - 2;
        StringBuilder ret = new StringBuilder();
        //处理第一行数据
        for(int i = 0; i < n; i += d) ret.append(s[i]);
        //处理中间 numRows-2 行数据
        for(int i = 1; i < numRows - 1; i++) {
            for(int j = i, k = d - j; j < n || k < n; j += d, k += d) {
                if(j < n) ret.append(s[i]);
                if(k < n) ret.append(s[k]);
            }
        }
        //处理最后一行数据
        for(int i = numRows - 1; i < n; i += d) ret.append(s[i]);

        return ret.toString();
    }

    public int minNumberOfFrogs(String croakOfFrogs) {
        String s = "croak";
        int n = s.length();
        Map<Character,Integer> map = new HashMap<>();
        //这个哈希表中存放叫声对应的字符和下标
        for(int i = 0; i < n; i++) {
            map.put(s.charAt(i),i);
        }
        //这个哈希表用来统计字符出现的次数，也就是模拟出蛙叫的过程
        int[] hash = new int[n];
        for(int i = 0; i < croakOfFrogs.length(); i++) {
            char ch = croakOfFrogs.charAt(i);
            if(map.get(ch) == 0) {
                if(hash[n - 1] != 0) {
                    hash[n - 1]--;
                    hash[0]++;
                }else {
                    hash[0]++;
                }
            }else {
                if(hash[map.get(ch) - 1] == 0) return -1;
                hash[map.get(ch) - 1]--;
                hash[map.get(ch)]++;
            }
        }

        for(int i = 0; i < n - 1; i++) {
            if(hash[i] != 0) return -1;
        }

        return hash[n-1];
    }
}
